package hit.example.Menu;

import hit.example.delegate.IOnMenuClosed;
import hit.example.hitdict.R;
import hit.example.home.HomeActivity;
import hit.example.home.RecentFabvoriteFm;
import hit.example.home.SearchVietAnhFm;
import hit.example.home.TabId;
import hit.example.home.TranslateFm;
import hit.example.net.ConnectionDetector;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.telpoo.frame.delegate.Idelegate;

public class ViewMenu extends Activity implements IOnMenuClosed,
		OnClickListener {
	Context context;
	Idelegate idelegate;
	View v;
	TextView Tudatra;
	View menuTudatra, menuTuYeuThich, menuSearchVA, menuSearchWord,
			menuTranslate, menuExit, menuSetting;
	ConnectionDetector cd;
	boolean isInternet = false;

	public ViewMenu(Context context1, Idelegate idelegate1) {
		this.context = context1;
		this.idelegate = idelegate1;

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v = inflater.inflate(R.layout.layout_menu, null);
		// khai bao view o day
		menuTudatra = v.findViewById(R.id.menuRecentWord);
		menuTuYeuThich = v.findViewById(R.id.menuFavoriteWord);
		menuSearchVA = v.findViewById(R.id.menuSearchVA);
		menuSearchWord = v.findViewById(R.id.menuSearchWord);
		menuTranslate = v.findViewById(R.id.menuTranslate);
		menuSetting = v.findViewById(R.id.menusetting);
		menuExit = v.findViewById(R.id.menuExitApp);
		((HomeActivity) context1).setDelegateOnMenuClosed(this);// dong menu va

		cd = new ConnectionDetector(context1);
		// ve home
		menuSetting.setOnClickListener(this);
		menuTudatra.setOnClickListener(this);
		menuTuYeuThich.setOnClickListener(this);
		menuSearchVA.setOnClickListener(this);
		menuTranslate.setOnClickListener(this);
		menuSearchWord.setOnClickListener(this);
		menuExit.setOnClickListener(this);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		cd = new ConnectionDetector(getApplicationContext());
		isInternet = cd.isConnectingToInternet();

	}

	public View getView() {

		return v;
	}

	@Override
	public void onMenuClosed() {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.menuSearchWord:
			HomeActivity.getInstanceMenu().pusListWordFm();
			HomeActivity.getInstance().toggle();
			break;
		case R.id.menuRecentWord:

			HomeActivity.getInstance().pushFragments(TabId.home,
					RecentFabvoriteFm.getInstance(0), true, null);
			HomeActivity.getInstance().toggle();
			break;

		case R.id.menuFavoriteWord:
			HomeActivity.getInstance().pushFragments(TabId.home,
					RecentFabvoriteFm.getInstance(1), true, null);
			HomeActivity.getInstance().toggle();
			break;
		case R.id.menuSearchVA:
			if (cd.isConnectingToInternet()) {
				HomeActivity.getInstance().pushFragments(TabId.home,
						SearchVietAnhFm.getInstance(), true, null);
				HomeActivity.getInstance().toggle();
			} else {
				showAlertDialog(context, "Warning ! ",
						"Kiểm tra kết nối internet", false);

			}
			break;
		case R.id.menuTranslate:

			if (cd.isConnectingToInternet()) {
				HomeActivity.getInstance().pushFragments(TabId.home,
						TranslateFm.getInstance(), true, null);
				HomeActivity.getInstance().toggle();
			} else {
				showAlertDialog(context, "Warning ! ",
						"Kiểm tra kết nối internet", false);

			}
			break;
		case R.id.menuExitApp:
			HomeActivity.getInstance().exitMenu();
			break;
		}

	}

	public void showAlertDialog(Context context, String title, String message,
			Boolean status) {
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();

		// Setting Dialog Title
		alertDialog.setTitle(title);

		// Setting Dialog Message
		alertDialog.setMessage(message);

		// Setting alert dialog icon
		alertDialog.setIcon((status) ? R.drawable.load : R.drawable.sound);

		// Setting OK Button
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				HomeActivity.getInstance().toggle();
			}
		});

		// Showing Alert Message
		alertDialog.show();
	}
}
